<template>
  <div>
    <div style="display: flex;flex-direction: column">
      <div style="display: flex">
        <div>
          <el-upload action="" :on-preview="handlePreview" :on-change="handleChange"
            :show-file-list="false" accept=".mp4" :auto-upload="false">
            <div style="display: flex;align-items: center">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            </div>
          </el-upload>
        </div>
        <div>
          <el-tooltip :content="content" placement="top">
            <i class="el-icon-question" style="color: red"/>
          </el-tooltip>
        </div>
      </div>
      <div style="display: flex;flex-direction: column;align-items: flex-start" v-if="!isBlank($store.state.videoOneUpload)">
        <video :src="$store.state.videoOneUpload.url" autoplay style="width: 120px;height: 120px;"/>
        <div style="width: 200px">{{ $store.state.videoOneUpload.name }}</div>
        <div style="margin-bottom: 5px;width: 200px;display: flex">
          <el-progress v-if="$store.state.uploadProgress>0" style="width: 180px;" :percentage="percentage" :color="customColorMethod"/>
        </div>
        <div style="display: flex">
          <el-button type="primary" round size="mini" :disabled="isFlag(percentage,100)" @click="handleUpload()">上传</el-button>
          <el-button type="danger" round size="mini" @click="handleRemove()">删除</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import appUtils, {formatDate, isBlank, isFlag, moneyFormat, ossMp4Put} from '@/utils/util'
import state from '@/store/index'

export default {
  name: 'videoOneUpload',
  props: ['videoUrl', 'size'],
  filters: {},
  data() {
    return {
      myImage: appUtils.myImage,
      content: "最大限制" + this.size + "MB",
      videoOneUpload: {},
      percentage: 0,
    }
  },
  watch: {
    "$store.state.uploadProgress": function (data) {
      this.percentage = data
    },
  },
  methods: {
    isFlag,
    isBlank,
    formatDate,
    handleChange(file, fileList) {
      this.handleMaxSize(file, this.size).then(res => {
        if (isFlag(res, true)) {
          fileList.forEach(value => {
            if (!isBlank(value.uid)) {
              this.videoOneUpload = {
                url: URL.createObjectURL(value.raw) + '',
                name: value.name,
                raw: value.raw,
              }
              this.$store.state.videoOneUpload = this.videoOneUpload
              this.$store.state.uploadProgress = 0
            }
          })
        } else {
          return this.$message.info('文件过大，请重新选择')
        }
      })
    },
    customColorMethod(percentage) {
      if (percentage < 30) {
        return '#909399';
      } else if (percentage < 70) {
        return '#e6a23c';
      } else {
        return '#67c23a';
      }
    },
    format(percentage) {
      return percentage === 100 ? '满' : `${percentage}%`;
    },
    handleMaxSize(file, size) {
      if (isBlank(size) || isFlag(size, 0)) {
        size = 50
      }
      const fileSize = file.size / 1024 / 1024 < size
      return new Promise((resolve, reject) => {
        if (!fileSize) {
          this.$message.info('文件过大,请不要超过' + size + 'M.')
          return resolve(false)
        } else {
          return resolve(true)
        }
      })
    },
    handleUpload() {
      const videoOneUpload = this.$store.state.videoOneUpload
      let videoUrl = videoOneUpload.name
      if (!isBlank(videoOneUpload.raw)) {
        videoUrl = ossMp4Put(videoOneUpload.raw, appUtils.windowImg)
      }
    },
    handleRemove() {
      this.$store.state.videoOneUpload = {}
      this.videoOneUpload = {}
    },
    handlePreview(item) {
      window.open(URL.createObjectURL(item.raw))
    }
  }
}
</script>

<style scoped>

</style>
